
#include <VolumeUnit.h>
#include <Formatter.h>
#include <algorithm>
#include <ostream>

#define BASE_UNIT         TSP
#define TSP_TO_BASE_UNIT 1
#define TBSP_TO_BASE_UNIT (3*TSP_TO_BASE_UNIT)
#define OZ_TO_BASE_UNIT (2*TBSP_TO_BASE_UNIT)

#define TSP_NAME "TSP"
#define TBSP_NAME "TBSP"
#define OZ_NAME "OZ"

namespace{
bool greaterThan(const Unit * lhs, const Unit * rhs)
{
	return *lhs > *rhs;
}
}


const VolumeUnit& VolumeUnit::getOz()
{
    static const VolumeUnit oz(OZ_TO_BASE_UNIT, OZ_NAME, E_VOLUME_TYPE);
    return oz;
}

const VolumeUnit& VolumeUnit::getTbsp()
{
    static const VolumeUnit tbsp(TBSP_TO_BASE_UNIT, TBSP_NAME, E_VOLUME_TYPE);
    return tbsp;
}

const VolumeUnit& VolumeUnit::getTsp()
{
    static const VolumeUnit tsp(TSP_TO_BASE_UNIT, TSP_NAME, E_VOLUME_TYPE);
    return tsp;
}

VolumeUnit::VolumeUnit(int factor, string name, const eUnitType unitType)
	:Unit(factor, name, unitType)
{
}

const Unit& VolumeUnit::getBaseUnit() const
{
	return BASE_UNIT;
}


void VolumeUnit::appendAndSortUnit(OrderedUnits & orderedunits, const Unit & unit) const
{
	orderedunits.push_back(&unit);
	std::sort(orderedunits.begin(), orderedunits.end(), greaterThan);
}


void VolumeUnit::printOrderedUnitsList(const Formatter& formatter)const
{
	OrderedUnits orderedUnits;
	appendAndSortUnit(orderedUnits, OZ);
	appendAndSortUnit(orderedUnits, TBSP);
	appendAndSortUnit(orderedUnits, TSP);
	
	for_each(orderedUnits.begin(), orderedUnits.end(), formatter);
}

